package org.ezplatform.office.forum.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.ezplatform.core.common.WebUser;
import org.ezplatform.core.dao.jpa.JpaBaseDao;
import org.ezplatform.core.pagination.PropertyFilter;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.office.forum.dao.ForumIntegralRecordDao;
import org.ezplatform.office.forum.entity.ForumIntegralRecord;
import org.ezplatform.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * @desc 论坛积分扣减记录Service
 * @create 2017-11-15 10:16
 **/
@Service
@Transactional
public class ForumIntegralRecordService extends BaseService<ForumIntegralRecord, String> {

    private final Logger log = LoggerFactory.getLogger(ForumIntegralRecordService.class);
    
    @Autowired
    private ForumIntegralRecordDao forumIntegralRecordDao;
    
    @Override
    protected JpaBaseDao<ForumIntegralRecord, String> getEntityDao() {
        return forumIntegralRecordDao;
    }
    
	/**
	 * 获取积分扣减记录翻页数据
	 * 
	 * @param localPageable
	 * @param localList
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public Page<Map> findByPage(Pageable pageable, List<PropertyFilter> propertyFilter,String type,String startTime,String endTime){
		log.debug("<--------获取积分扣减记录翻页数据------->");
		StringBuilder sqlStr = new StringBuilder();
		Map<String, Object> paramMap = new HashMap<String, Object>(); 
		sqlStr.append("select t.id,t.createdDate,t.useraccount,t.username,t.reduce_score,t.total_score,t.reduce_account,t.reduce_mark from oa_forum_integral_record t where 1=1 ");
		if("user".equals(type)) {
			WebUser user = WebUtils.getCurrentUser();
			sqlStr.append(" and t.userid = :userid ");
			paramMap.put("userid",user.getUserId());
		}
		//起止时间查询
		if(StringUtils.isNotBlank(startTime)) {
			startTime = startTime + " 00:00:00";
			Date start = null;
			try {
				SimpleDateFormat  sdf1 =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				start = sdf1.parse(startTime);
			}catch(Exception e) {
				e.printStackTrace();
			}
			sqlStr.append(" and t.createdDate >= :startTime ");
			paramMap.put("startTime", start);
		}
		if(StringUtils.isNotBlank(endTime)) {
			endTime = endTime + " 23:59:59";
			Date end = null;
			try {
				SimpleDateFormat  sdf2 =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				end = sdf2.parse(endTime);
			}catch(Exception e) {
				e.printStackTrace();
			}
			sqlStr.append(" and t.createdDate <= :endTime ");
			paramMap.put("endTime", end);
		}
		String sql = PropertyFilter.getSqlByFilterAndPageable("t", sqlStr.toString(), "t.createdDate desc",propertyFilter, pageable);		
		return super.findByPageNativeSQL(pageable, sql, paramMap);
	}
	
	public ForumIntegralRecord editSave(ForumIntegralRecord forumIntegralRecord) {
		return this.save(forumIntegralRecord);
	}
    
}
